package cc.chengheng.config;

import cc.chengheng.service.UserService;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * 授权服务器的配置 oauth2
 * 主要配置的就是客户端，怎么获取授权码
 */
@Configuration
@EnableAuthorizationServer
public class AuthorizationServer extends AuthorizationServerConfigurerAdapter {

    @Resource
    private PasswordEncoder passwordEncoder;

    @Resource
    private UserService userService;

    @Resource
    private AuthenticationManager authenticationManager;

    @Resource
    private TokenStore jwtTokenStore;

    @Resource
    private JwtAccessTokenConverter jwtAccessTokenConverter;

//    @Resource
//    private TokenStore tokenStore;

    @Resource
    private MyTokenEnchaner myTokenEnchaner;

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        //        扩展jwt内容
        ArrayList<TokenEnhancer> delegates = new ArrayList<>();
        delegates.add(myTokenEnchaner);
        delegates.add(jwtAccessTokenConverter);
        TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
        tokenEnhancerChain.setTokenEnhancers(delegates);

        endpoints
                // 自定义登录逻辑
                .userDetailsService(userService)

                // 认证管理器
                .authenticationManager(authenticationManager)

                // 令牌存储位置
                .tokenStore(jwtTokenStore)

                // 转成jwt
                .accessTokenConverter(jwtAccessTokenConverter)

                .tokenEnhancer(tokenEnhancerChain);
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

        clients.inMemory()
                // clientId
                .withClient("admin")
                .secret(passwordEncoder.encode("fengliulin"))

                // 访问令牌失效时间
                .accessTokenValiditySeconds(26000)

                // 重定向地址，用于获取授权码
                .redirectUris("http://127.0.0.1:8082/login")
                .scopes("all") // 授权范围
                // 自动授权，忽略掉那个网页
                .autoApprove(true)
                .authorizedGrantTypes("authorization_code", "password", "refresh_token") // 授权码模式、密码模式、刷新令牌
        ;
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.tokenKeyAccess("isAuthenticated()");
    }
}
